<template>
  <div class="login-container">
    <div class="login-left">
    </div>
    <div class="login-right">
      <div class="login-form">
        <h2 class="welcome">欢迎登录</h2>
        <p class="sub-title">智慧学习考核系统</p>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules" 
          class="form">
          <el-form-item prop="phone">
            <el-input
              v-model="loginForm.phone"
              placeholder="请输入手机号"
              :prefix-icon="Phone"/>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              :prefix-icon="Lock"/>
          </el-form-item>
          
          <div class="remember-forgot">
            <el-checkbox v-model="loginForm.remember">自动登录</el-checkbox>
            <el-link type="primary" @click="forgotPassword">忘记密码</el-link>
          </div>
          
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
          
          <div class="register">
            <el-link type="primary" @click="goToRegister">注册学员账号 ></el-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Phone, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { TokenUtil } from '@/utils/common.js'
import { userApi } from '@/api/modules/user.js'
import { useUserStore } from '@/stores/user.js'
import { useMenuStore } from "@/stores/menu";

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
const userStore = useUserStore();
const menuStore = useMenuStore();

const loginForm = reactive({
  phone: '',
  password: '',
  remember: false
})
const userInfo = computed(() => userStore.userInfo);

const loginRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    // { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      const data = await userApi.login({
        login: loginForm.phone,
        password:loginForm.password,
        // password: encryptedPassword,
        source: 'student',
        dev: 1
      });
      ElMessage.success('登录成功')
      TokenUtil.setToken(data.token);
      await userStore.fetchUserInfo(data);
      const type = userInfo.value.user_type
      if (type == '2' || type == '3' || type == '5') {
        menuStore.setActiveIndex('9');
        menuStore.setMenuType('4');
        router.push("/teacherAssessList");
      } else if(type == '4'){
        menuStore.setActiveIndex('10');
        menuStore.setMenuType('5');
        router.push("/associationAssessList");
      }else {
        menuStore.setActiveIndex('1');
        menuStore.setMenuType('1');
        router.push("/");
      }
    }
    loading.value = false;
  })
}

const forgotPassword = () => {
  router.push('/forgot-password')
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  background: #fff;
  padding: 8px;
  
  .login-left {
    flex: 3;
    padding: 40px;
    background-image: url('@/assets/images/login-bg.png');
    background-size: cover;
    position: relative;
    
    .logo-title {
      display: flex;
      align-items: center;
      gap: 12px;
      
      .logo {
        width: 40px;
        height: 40px;
      }
      
      span {
        font-size: 24px;
        font-weight: 500;
        color: #1890FF;
      }
    }
    
    .login-bg {
      width: 80%;
      max-width: 600px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
  
  .login-right {
    flex: 2;
    // width: 500px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .login-form {
      width: 360px;
      
      .welcome {
        font-family: MiSans, MiSans;
        font-weight: 600;
        font-size: 36px;
        color: rgba(0,0,0,0.88);
        line-height: 44px;
        letter-spacing: 7px;
        text-align: center;
        font-style: normal;
        text-transform: none;
        margin-bottom: 8px;
      }
      
      .sub-title {
        font-family: MiSans, MiSans;
        font-weight: 500;
        font-size: 24px;
        color: rgba(0,0,0,0.65);
        line-height: 32px;
        letter-spacing: 6px;
        text-align: center;
        font-style: normal;
        text-transform: none;
        margin-bottom: 40px;
      }
      :deep(.el-input__inner){
        border-left: 1px solid #eee;
        padding-left: 10px;
      }
      .form {
        .el-input {
          height: 40px;
          
          :deep(.el-input__inner) {
            height: 40px;
          }
        }
        
        .remember-forgot {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin: 16px 0 24px;
          
          .el-checkbox {
            color: #4E5969;
          }
        }
        
        .login-button {
          width: 100%;
          height: 40px;
          font-size: 16px;
          margin-bottom: 16px;
        }
        
        .register {
          font-family: MiSans, MiSans;
          font-weight: 400;
          font-size: 14px;
          text-align: right;
          :deep(.el-link__inner){
            color: rgba(0,0,0,0.65) !important;
            line-height: 22px;
            font-style: normal;
            text-transform: none;
          }
          
        }
      }
    }
  }
}

// 响应式布局
@media screen and (max-width: 768px) {
  .login-container {
    .login-left {
      display: none;
    }
    
    .login-right {
      width: 100%;
      padding: 20px;
      
      .login-form {
        width: 100%;
      }
    }
  }
}
</style>
